import java.util.ArrayList;

enum acctype {
	CASH, SENIOR_CITIZEN, SAVING, TEACHERS;
}

public class Account implements StockValueObserver {

	private int accountId = 0;
	private int portfolioId;
	private acctype accountType;
	private double balance;
	private String accountDesc;
	private ArrayList<AbstractOrder> orders = new ArrayList<AbstractOrder>();
	private int requestType;
	private int orderId = 0;
	private ArrayList<Stock> stocks = new ArrayList<Stock>();
	private ArrayList<MutualFund> mutualFunds = new ArrayList<MutualFund>();
	private static boolean toggle = false;

	public ArrayList<MutualFund> getMutualFunds() {
		return mutualFunds;
	}

	public void setMutualFunds(ArrayList<MutualFund> mutualFunds) {
		this.mutualFunds = mutualFunds;
	}

	public ArrayList<AbstractOrder> getOrders() {
		return orders;
	}

	public void setOrders(ArrayList<AbstractOrder> orders) {
		this.orders = orders;
	}

	public int getOrderId() {
		return orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	public ArrayList<Stock> getStocks() {
		return stocks;
	}

	public void setStocks(ArrayList<Stock> stocks) {
		this.stocks = stocks;
	}

	public Account() {
	}

	public int getAccountId() {
		return accountId;
	}

	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public acctype getAccountType() {
		return accountType;
	}

	public void setAccountType(acctype accountType) {
		this.accountType = accountType;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public String getAccountDesc() {
		return accountDesc;
	}

	public void setAccountDesc(String accountDesc) {
		this.accountDesc = accountDesc;
	}

	public int getRequestType() {
		return requestType;
	}

	public void setRequestType(int requestType) {
		this.requestType = requestType;
	}

	public void displayAccountDetails() {
		System.out.println("\nAccount Information");
		System.out.println("AccountId: " + accountId);
		System.out.println("balance: " + balance);
		System.out.println("Account Description " + accountDesc);
	}

	public void createAccount(int accountId, acctype accountType,
			int portfolioId) {
		this.accountId = accountId;
		this.accountType = accountType;
		this.portfolioId = portfolioId;
		balance = 100000;
		accountDesc = "This is a " + accountType + " account";
	}

	@Override
	public void updateOrder(StockValueObserver buyer,
			StockValueObserver seller, int matchedQuantity) {
		if (!toggle) {
			AbstractOrder buyerOrder = ((ActiveOrder) buyer).getOrder();
			AbstractOrder sellerOrder = ((ActiveOrder) seller).getOrder();

			Account buyerAccount = PortfolioRepository.getPortfolioRepository()
					.getAccount(buyerOrder.getAccountId());
			Account sellerAccount = PortfolioRepository
					.getPortfolioRepository().getAccount(
							sellerOrder.getAccountId());

			double price = 0;

			boolean isBuyerLimitOrder = OrderType.LIMIT.equals(buyerOrder
					.getOrderType());
			boolean isSellerLimitOrder = OrderType.LIMIT.equals(sellerOrder
					.getOrderType());

			System.out.println("BEFORE UPDATING ACCOUNT: ");
			System.out.println("\nBuyer Account: " + buyerAccount.getBalance());
			System.out.println("Seller Account: " + sellerAccount.getBalance());
			if (isBuyerLimitOrder && isSellerLimitOrder) {
				price = buyerOrder.getPriceLimit();
				price = (matchedQuantity * price);
				sellerAccount.setBalance(sellerAccount.getBalance() + price);
				buyerAccount.setBalance(buyerAccount.getBalance() - price);
			} else if (isBuyerLimitOrder && !isSellerLimitOrder) {
				price = buyerOrder.getPriceLimit();
				price = (matchedQuantity * price);
				sellerAccount.setBalance(sellerAccount.getBalance() + price);
				buyerAccount.setBalance(buyerAccount.getBalance() - price);
			} else if (!isBuyerLimitOrder && isSellerLimitOrder) {
				price = sellerOrder.getPriceLimit();
				price = (matchedQuantity * price);
				sellerAccount.setBalance(sellerAccount.getBalance() + price);
				buyerAccount.setBalance(buyerAccount.getBalance() - price);
			}

			System.out.println("After UPDATING ACCOUNT: ");
			System.out.println("\nBuyer Account: " + buyerAccount.getBalance());
			System.out.println("Seller Account: " + sellerAccount.getBalance());
			System.out.println("UPDATE Account!!!");
			toggle = true;
		} else {
			toggle = false;
		}
	}
}
